//
//  OrderCell.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/11/10.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit
import SwiftyJSON
class OrderModel: NSObject {
    var creatTime:String
    var customerAddres:String
    var customerPhone:String
    var customerName:String
    
    var orderNumber:String
    var orderID:String
    var ID:String
    var orderAddTime:String

    var orderStatus:String
    var orderType:String
    
    var payStatus:String
    var payType:String
    
    var postDate:String
    var postYear:String
    var postTime:String

    var deliveryEmail:String
    var deliveryUid:String
    var deliveryUsername:String
    var deliveryCost:String
    var deliveryWeight:String
    var deliveryType:String
    
    var shopID:String
    var shopAddress:String
    var shopName:String
    var shopPhone:String
    var shopType:String
    
    var status:String
    
    init(WithDict dict:[String:JSON]) {
        creatTime = dict["addtime"]!.stringValue
        customerAddres = dict["buyeraddress"]!.stringValue
        customerName = dict["buyername"]!.stringValue
        customerPhone = dict["buyerphone"]!.stringValue
        
        orderNumber = dict["dno"]!.stringValue
        ID = dict["id"]!.stringValue
        orderID = dict["orderid"]!.stringValue

        
        orderStatus = dict["orderstatus"]!.stringValue
        orderType = dict["ordertype"]!.stringValue
        
        payStatus = dict["paystatus"]!.stringValue
        payType = dict["paytype"]!.stringValue

        postDate = dict["postdate"]!.stringValue
        postYear = dict["postyear"]!.stringValue
        postTime = dict["pstime"]!.stringValue

        deliveryEmail = dict["psemail"]!.stringValue
        deliveryUid = dict["psuid"]!.stringValue
        deliveryUsername = dict["psusername"]!.stringValue
        deliveryCost = dict["psycost"]!.stringValue
        deliveryWeight = dict["ptkg"]!.stringValue
        deliveryType = dict["pttype"]!.stringValue
        
        shopID = dict["shopid"]!.stringValue
        shopAddress = dict["shopaddress"]!.stringValue
        shopName = dict["shopname"]!.stringValue
        shopPhone = dict["shopphone"]!.stringValue
        shopType = dict["shoptype"]!.stringValue
        status = dict["status"]!.stringValue
        orderAddTime = dict["addtime"]!.stringValue
    }
}

protocol orderCellClickDelegate {
    func handleOrder(WithDotType dotType:String, orderID:String)
}

class OrderCell: UITableViewCell {

    @IBOutlet weak var orderIncomeLabel: UILabel!
    @IBOutlet weak var postTimeLabel: UILabel!
    @IBOutlet weak var shopNameLabel: UILabel!
    @IBOutlet weak var orderStatusLabel: UILabel!
    
    @IBOutlet weak var shopAddressLabel: UILabel!
    @IBOutlet weak var customerAddressLabel: UILabel!
    
    @IBOutlet weak var orderCreatTimeLabel: UILabel!
    @IBOutlet weak var orderIDLabel: UILabel!
    
    @IBOutlet weak var btn: UIButton!
    
    var thisModel:OrderModel?

    var thisDelegate:orderCellClickDelegate?
    
    @IBOutlet weak var customerAddressBottom: NSLayoutConstraint!//默认的53.5 有按钮，无按钮8.5
    
    func setInterFace(WithModel model:OrderModel)
    {
        thisModel = model
        orderIncomeLabel.text = model.deliveryCost
        postTimeLabel.text = model.postDate
        shopNameLabel.text = model.shopName
        orderStatusLabel.text = "(" + model.orderStatus + ")"
        shopAddressLabel.text = model.shopAddress
        customerAddressLabel.text = model.customerAddres
        orderIDLabel.text = model.orderID
        orderCreatTimeLabel.text = model.orderAddTime
        
        btn.layer.cornerRadius = 5
        //判断按钮
        self.setBtnStatus()
    }
    
    func setBtnStatus()
    {
        switch thisModel!.status {
        case "0":
            btn.setTitle("抢单", for: UIControlState.normal)
            btn.backgroundColor = MainGreenColor
            btn.isEnabled = true

        case "1":
            btn.setTitle("已取货", for: UIControlState.normal)
            btn.backgroundColor = MainGreenColor
            btn.isEnabled = true

        case "2":
            btn.setTitle("已送达", for: UIControlState.normal)
            btn.backgroundColor = MainGreenColor
            btn.isEnabled = true
        case "3":
            btn.setTitle("已完成", for: UIControlState.normal)
            btn.backgroundColor = UIColor.gray
            btn.isEnabled = false
        default:
            btn.setTitle("已结束", for: UIControlState.normal)
            btn.backgroundColor = UIColor.gray
            btn.isEnabled = false
        }
    }
    
    @IBAction func handleOrder(_ sender: Any) {
        
        
        var dotType = ""
        
        
        switch thisModel!.status {
        case "0":
            dotType = "joinorder"
        case "1":
            dotType = "pickorder"
        case "2":
            dotType = "sendorder"
        case "3":
            dotType = ""
        default:
            dotType = ""
        }
        
        if dotType.characters.count == 0
        {
            self.wkMakeToastToCenter(message: "此订单不可操作,请联系技术人员询问")
            return
        }
        
        
        if (thisDelegate != nil)
        {
            thisDelegate?.handleOrder(WithDotType: dotType, orderID: thisModel!.orderID)
        }
        
//        NetWorkingManager.handleOrder(WithID: thisModel!.orderID, dotType: dotType,hintStr:nil, block: {(state, data) in
//            switch state
//            {
//            case .Faild(let msg):
//                self.wkMakeToastToCenter(message: msg)
//            case .Success:
//                print("成功")
//                self.thisModel!.status = "\((self.thisModel!.status as NSString).intValue + 1)"
//                self.setBtnStatus()
//            }
//        })
    }

    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.selectionStyle = .none
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
